using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Xml.Serialization;
using WozLib.Xml;


namespace WozLib.UI.Ink.SketchCommands
{
  public class WindowRenameSketchCommand : RememberSketchCommand
  {
    [XmlElement("NewDescription")]
    public string NewDescription
    {
      get { return _NewDescription; }
      set { _NewDescription = value; }
    } private string _NewDescription;

    [XmlElement("PreviousDescription")]
    public string PreviousDescription
    {
      get { return _PreviousDescription; }
      set { _PreviousDescription = value; }
    } private string _PreviousDescription;

    [XmlElement("RememberCommand")]
    public bool RememberCommand
    {
      get { return _RememberCommand; }
      set { _RememberCommand = value; }
    } private bool _RememberCommand;

    public WindowRenameSketchCommand(string newDescription, string previousDescription)
      : base()
    {
      NewDescription = newDescription;
      PreviousDescription = previousDescription;
      RememberCommand = false;
    }

    public WindowRenameSketchCommand(string newDescription)
      : base()
    {
      NewDescription = newDescription;

      // todo keep track that we need to save and load this ourself
      RememberCommand = true;
      RegisterObject();
    }

    public WindowRenameSketchCommand()
      : base()
    {
    }

    public override void Redo()
    {
      if (this.WozClientCommandReferences != null)
      {
        if (this.WozClientCommandReferences.ClientWindow != null)
        {
          if (RememberCommand)
          {
            RegisterObject();
            PreviousDescription = this.WozClientCommandReferences.ClientWindow.Title;
          }
          this.WozClientCommandReferences.ClientWindow.Title = NewDescription;
        }
      }
    }

    public override void Undo()
    {
      if (RememberCommand)
      {
        RememberSketchCommand wcrc = GetCommand(WozClientClearCommandID);
        if (wcrc != null && wcrc is WindowRenameSketchCommand)
        {
          WindowRenameSketchCommand wrsc = wcrc as WindowRenameSketchCommand;
          if (wrsc.WozClientCommandReferences != null)
          {
            if (wrsc.WozClientCommandReferences.ClientWindow != null)
            {
              wrsc.WozClientCommandReferences.ClientWindow.Title = wrsc.PreviousDescription;
            }
          }
        }
      }
      else
      {
        if (this.WozClientCommandReferences != null)
        {
          if (this.WozClientCommandReferences.ClientWindow != null)
          {
            this.WozClientCommandReferences.ClientWindow.Title = PreviousDescription;
          }
        }
      }
    }
  }

}
